using System;
using System.Web; //Need this for our HTTPCookie class
using System.Web.Security; // Very important namespace for this app. Don't forget it.
namespace ASPAuthors.aspnetbyexample.ch10
{
public class Chapter10LoginCS : System.Web.UI.Page
{
// Our code behind page will inherit the System's Page object and the;px
// page which presents our data will inherit the event handlers in this file
// We will capture the events that occur within our;px page and, well, handle them.
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.TextBox txtPass;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.TextBox txtCookie;
protected System.Web.UI.WebControls.RadioButton btnYes;
protected System.Web.UI.WebControls.RadioButton btnNo;
protected System.Web.UI.WebControls.Label lblCookie;
protected System.Web.UI.WebControls.Label lblSessionID;
protected System.Web.UI.WebControls.Label lblCookie2;
protected System.Web.UI.WebControls.TextBox txtCookie2;
protected System.Web.UI.WebControls.Button cmdLogin;
private void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
}
#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Page_Load(Object sender, EventArgs e)
{
lblSessionID.Text = "Your Session ID is " + Session.SessionID;
System.Web.HttpCookie objCookie; // By importing the System.Web namespace you can create an instance of the HTTPCookie Class
objCookie = Request.Cookies["Chapter10"];
if(objCookie != null)
{//If the value is not null then we can set them accordingly
lblMessage.ForeColor = System.Drawing.Color.Black;
lblMessage.Text = "Welcome back, I recognized you by your childish cookie! Please login to view Chapter10 online.";
}
else if(Session["YourName"] == "")
{
// We're checking the Session Object data to see if that variable it contains any string data.
// If it it is empty it's pretty safe to;sume they haven't tried to login yet. I know, it's an ugly little hack.
// I don't even need HTML right now! I just plopped a textbox object on the page.
lblMessage.ForeColor = System.Drawing.Color.Black;
lblMessage.Text = "Welcome, please login to view Chapter10 online.";
}
else
{
// Ok, something's strange here! They have a string data in their Session("YourName") variable. They must have been
// sent back to us by the login page
lblMessage.ForeColor = System.Drawing.Color.Black;
lblMessage.Text = "Unauthorized Access: I'm sorry " +
Session["YourName"] +
", please check your username and password and try again.";
}
}
private void cmdLogin_Click(Object sender, EventArgs e)
{
if(txtName.Text.Length <= 0)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Please enter a username.";
return;
}
if(txtPass.Text.Length <= 0)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Please enter a password.";
return;
}
if(btnYes.Checked)
{
Response.Cookies["Chapter10"]["Childish"] = txtCookie.Text;
Response.Cookies["Chapter10"]["Mature"] = txtCookie2.Text;
Response.Cookies["Chapter10"].Expires = System.DateTime.Parse("3/3/2039");
}
if(FormsAuthentication.Authenticate(txtName.Text, txtPass.Text))
{
// Excellent, they are authorized and authenticated
Session["YourName"] = txtName.Text;
// Send them to the a Chapter10text page
Response.Redirect("Chapter10Text.aspx?Name=" + txtName.Text);
}
else
{
// Ok, at least we have their name and can create a more personalized message.
lblMessage.ForeColor = System.Drawing.Color.Black;
lblMessage.Text = "Unauthorized Access: I'm sorry " + Session["YourName"] +
", please check your username and password and try again.";
}
}
// NOTE: By;signing both of these radio buttons a shared group name .NET treat them like a control array
private void btnYes_CheckedChanged(Object sender, EventArgs e)
{
// As soon as they select one of my annoying required radio buttons, I enable the login button
// This is not exactly an ideal example of what we can do using postback features, but
// it should at least spark some good ideas.
cmdLogin.Enabled = false;
lblCookie.Visible = true;
txtCookie.Visible = true;
lblCookie2.Visible = true;
txtCookie2.Visible = true;
}
private void btnNo_CheckedChanged(Object sender, EventArgs e)
{
// I'll use this event to enable the login button. It's default "enabled"
// property is set to false
cmdLogin.Enabled = true;
lblCookie.Visible = false;
txtCookie.Visible = false;
lblCookie2.Visible = false;
txtCookie2.Visible = false;
}
private void txtCookie_TextChanged(Object sender, EventArgs e)
{
cmdLogin.Enabled = true;
}
}
}